
#!/bin/bash
#准备阶段
#拷贝行为树头文件
funIncBehar (){
cp -fr /usr/local/include/behaviortree_cpp ./envs/inc/
cp -fr ~/AppComm/inc/tinyxml ./envs/inc/
cp -fr ~/AppComm/inc/json ./envs/inc/
}
#拷贝gstreamer 头文件
funIncGst (){
cp -fr /usr/local/include/gstreamer-1.0 ./envs/inc/
}

#拷贝opencv 头文件
funIncOpenCV (){
cp -fr /usr/local/include/opencv ./envs/inc/
cp -fr /usr/local/include/opencv2 ./envs/inc/
}


#创建目录存放程序
if [ -f ./tmp ]
then
rm ./tmp
fi

if [ ! -d ./envs ]
then
  mkdir envs
  mkdir -p ./envs/nodelibs #创建目录存放依赖库
  mkdir -p ./envs/inc #创建目录存放三方库头文件
fi
#包含行为节点依赖头文件
echo "包含行为树库头文件"
funIncBehar
#包含opencv头文件
echo "包含opencv头文件"
funIncOpenCV
#包含gstreamer头文件
echo "包含gstreamer头文件"
funIncGst

#打包q10驱动
if [ -d ~/cap_4328 ]
then
echo "cp ~/cap_4328"
cp -fr ~/cap_4328 ./envs/
else
echo "缺少q10驱动"
exit 0
fi


echo "检测全部依赖库..."

ldd ~/ros2_ws/install/q10f_pkg/lib/q10f_pkg/q10f_node | awk '{print $3}' >> ./tmp #导出吊舱接入节点依赖库
ldd ~/ros2_ws/install/mavros/lib/mavros/mavros_node | awk '{print $3}' >> ./tmp #导出mavros依赖库
ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/fcu_comm_node | awk '{print $3}' >> ./tmp #导出飞行控制节点依赖库
#ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/follow_attack_node | awk '{print $3}' >> ./tmp
ldd ~/ros2_ws/install/fcu_comm/lib/fcu_comm/vision_locate_node | awk '{print $3}' >> ./tmp #导出目标位置解算依赖库
ldd ~/ros2_ws/install/dds2ros/DDS2ROS | awk '{print $3}' >> ./tmp #导出DDS2ROS依赖库
ldd ~/ros2_ws/install/behavior-process/lib/behavior-process/behavior-node | awk '{print $3}' >> ./tmp #导出行为节点依赖库
ldd ~/ros2_ws/install/usb_cam_pkg/lib/usb_cam_pkg/usb_cam_node | awk '{print $3}' >> ./tmp #导出usb相机接入依赖库
ldd ~/ros2_ws/install/video_distribution/lib/video_distribution/video_distribution | awk '{print $3}' >> ./tmp #导出视频分发节点依赖库
ldd ~/ros2_ws/install/new_cluster/lib/new_cluster/Cluster_formation_control_node_new | awk '{print $3}' >> ./tmp #导出集群编队依赖库
ldd ~/ros2_ws/install/xe_thru/lib/xe_thru/x4m300 | awk '{print $3}' >> ./tmp #导出生命探测雷达接入节点依赖库

echo "拷贝全部依赖库..."
if [ -f ./tmp ]
then
while read line
do
  ros=$(echo $line | grep "/opt/ros")
  custom=$(echo $line | grep "libcustom_msgs")
  
  if [  "$ros" == ""  -a  "$custom" == "" ] #如为ros库文件和自定义消息库文件则不拷贝，单独拷贝了ros库，自定义消息在打包节点时包含
  then

    filepath="$line"
    #items=(${line////})
    len=${#filepath}
 
    #cp $line ./test/${items[$len-1]}
    if [ ! $len -eq 0 ]
    then
    
      filename=$(basename $line)

      if [  -f ./envs/nodelibs/${filename} ] #判定库文件是否不存在，不存在时才拷贝
      then
        continue
      fi
      echo "copy file $line"
      cp -n $line ./envs/nodelibs/
      status=$?
      if [ ! $status -eq 0 ]
      then
        echo "cp file $line failed"
      fi

    fi

  fi
done < ./tmp

rm ./tmp
fi


cd envs
#pack yolo
#depend module 拷贝yolo依赖库
if [ -d ~/yolo-depend ]
then
echo "copy yolodepend from ~/yolo-depend"
cp ~/yolo-depend/* ./
else
echo "no yolo depend path ~/yolo-depend"
fi
#run_model拷贝yolo模型
if [ -d ~/run_model ]
then
echo "cp ~/run_model"
cp -fr ~/run_model ./
#zip -r ./run_model.zip ~/run_model
else
echo "no run_model for yolo"
fi
cd ..



#pack ros 拷贝ros环境
cd envs
tar -czvf foxy.tar.gz /opt/ros/foxy
cd ..



tar -czvf envs.tar.gz envs
cat distribution-env-dev-install.sh envs.tar.gz > distribution-env-dev-install.run

chmod +x distribution-env-dev-install.run

rm -fr envs
rm -fr envs.tar.gz
echo "打包完成"

